import time

from selenium.webdriver.common.by import By

import page
from base.app_base import AppBase


class ZoPageSearchHistory(AppBase):
    # 1.点击全局搜索按钮，输入关键字进行搜索
    def page_click_search_button(self, search_text):
        self.base_click(page.zo_serarch_button)
        self.base_input(page.zo_search_input, search_text)
        self.base_click(page.zo_search_input)
        self.app_base_click_search()
        time.sleep(1)
        # 点击取消
        self.base_click(page.zo_search_cancel_button)
        # 再次进入全局搜索
        self.base_click(page.zo_serarch_button)

    # 查看历史搜索
    def page_find_search_history(self):
        result = self.base_get_text(page.zo_search_history_01)
        # 再次点击取消
        self.base_click(page.zo_search_cancel_button)
        return result

    # 查看历史搜索记录 最多十条
    def page_check_history_search_recorder(self):
        # 进入全局搜索
        self.base_click(page.zo_serarch_button)
        # 搜索记录条数
        count = 0
        # 循环遍历历史搜素记录
        for i in range(2, 13):
            # 历史搜索配置信息
            loc = By.XPATH, "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup/android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup[2]/androidx.viewpager.widget.ViewPager/androidx.recyclerview.widget.RecyclerView/android.widget.FrameLayout/android.view.ViewGroup/android.widget.ScrollView/android.view.ViewGroup/android.view.ViewGroup[{}]/android.widget.TextView".format(i)
            exist = self.app_base_is_exist(loc)
            time.sleep(0.3)
            if exist:
                i = i + 1
                count = count + 1
        print(i)
        print(count)
        if count <= 10:
            return True
        else:
            return False

    # 组合业务方法
    def zo_page_search_history(self, search_text):
        self.page_click_search_button(search_text)


